package serversidepkg;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import backendpkg.DataSource;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * Servlet implementation class CloseChat
 */
public class CloseChat extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CloseChat() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
			response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
			response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
			response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
			
			//Get username from session
			HttpSession session = request.getSession();			
            String user = (String) session.getAttribute("username");

            if (user == null) {
               request.setAttribute("Error", "Session has ended.  Please login.");
               RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
               rd.forward(request, response);
            }else{
            
	            //Get Parameters from request
	            String user2 = request.getParameter("user2");

	            
	            //Get connection to database
	            DataSource datasource = new DataSource();
	            Connection conn = datasource.getConnection();
	            PreparedStatement statement;
            	
            	//Delete from incoming
	            String deleteinc ="DELETE FROM Incoming WHERE receiver=? AND sender=? OR receiver=? AND sender=?";
	            
	            statement = (PreparedStatement) conn.prepareStatement(deleteinc);
	            statement.setString(1, user);
	            statement.setString(2, user2);
	            statement.setString(3, user2);
	            statement.setString(4, user);
	            statement.executeUpdate();
	        
	            //Delete from Chat
	            String deletechat ="DELETE FROM Chat WHERE user1=? AND user2=? OR user1=? AND user2=?";
                
                statement = (PreparedStatement) conn.prepareStatement(deletechat);
                statement.setString(1, user);
                statement.setString(2, user2);
                statement.setString(3, user2);
                statement.setString(4, user);
                statement.executeUpdate();
       
	            conn.close();
            }
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            
        }
	}

}
